using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DotNetWarcraft.WoWGuildRoster.Business;

namespace DotNetWarcraft.WoWGuildRoster.UI.Windows {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    String MessageBoxCharString(GuildEntry _entry) {

      return "Character: " + _entry.Char_Name + " (ID: " + _entry.Char_ID + ")\r\n" +
              "    Current Flag: " + _entry.Current_Flag + "\r\n" +
              "    Guild: " + _entry.Char_Guild + "\r\n" +
              "    Server: " + _entry.Char_Server + "\r\n" +
              "    Region: " + _entry.Char_Region + "\r\n" +
              "    Gender/Race: " + _entry.Char_Gender + " " + _entry.Char_Race + "\r\n" +
              "    Class: " + _entry.Char_Class + ", level " + _entry.Char_Level + "\r\n" +
              "    Spec: " + _entry.Char_Spec + "\r\n" +
              "    Spec #s: " + _entry.Char_Spec1_Level + "/" + _entry.Char_Spec2_Level + "/" + _entry.Char_Spec3_Level;
    }

    String MessageBoxCharStatString(GuildEntryStat _stat) {

      return "[ID: " + _stat.CharStat_ID + "]\r\n" +
              "    Character: " + _stat.Char_ID + "\r\n" +
              "    Stat Type: " + _stat.Stat_Type + "\r\n" +
              "    Group: " + _stat.Stat_Group + "\r\n" +
              "    Entry: " + _stat.Stat_Entry + "\r\n" +
              "    Value: " + _stat.Stat_Value_int + "/" + _stat.Stat_Value_str;
    }


    private void button1_Click(object sender, EventArgs e) {
      try {
        List<Business.GuildRoster> _rosters;
        _rosters = Business.GuildRosterController.GetRosters();

        if (_rosters.Count == 0) {
          MessageBox.Show("No guild rosters were returned.");
        } else {
          foreach (Business.GuildRoster _roster in _rosters) {
            MessageBox.Show("Returned Guild:" + _roster.Guild_Name + " (ID: " + _roster.Roster_ID + ")\r\n" +
              "    Server: " + _roster.Guild_Server + "\r\n" + "    Region: " + _roster.Guild_Region);
          }
        }
      } catch (Exception eExc) {
        MessageBox.Show(eExc.GetType().ToString() + ": \r\n" + eExc.Message.ToString());
      }
    }

    private void button2_Click(object sender, EventArgs e) {
      try {
        Business.GuildRoster _roster = new DotNetWarcraft.WoWGuildRoster.Business.GuildRoster();
        _roster.Guild_Name = "Test Guild";
        _roster.Guild_Server = "Whisperwind";
        _roster.Guild_Region_Enum = WarcraftRegions.RegionUS;
        _roster.Last_Update_Date = DateTime.UtcNow;

        _roster.Roster_ID = Business.GuildRosterController.AddRoster(_roster);

        if (_roster.Roster_ID > 0) {
          MessageBox.Show("Guild Creation Succeeded");
        } else {
          MessageBox.Show("Guild Creation Failed");
        }

        Business.GuildRoster _roster2;
        _roster2 = Business.GuildRosterController.GetRoster(_roster.Roster_ID);

        if (_roster2 == null) {
          MessageBox.Show("Post add: No guild rosters were returned.");
        } else {
          MessageBox.Show("Post add: Returned Guild:" + _roster2.Guild_Name + " (ID: " + _roster2.Roster_ID + ")\r\n" +
            "    Server: " + _roster2.Guild_Server + "\r\n" + "    Region: " + _roster2.Guild_Region);
        }

        _roster2.Guild_Name = "Test Guild Updated";
        _roster2.Last_Update_Date = DateTime.UtcNow;

        Business.GuildRosterController.UpdateRoster(_roster2);

        _roster2 = Business.GuildRosterController.GetRoster(_roster.Roster_ID);

        if (_roster2 == null) {
          MessageBox.Show("Post update: No guild rosters were returned.");
        } else {
          MessageBox.Show("Post update: Returned Guild:" + _roster2.Guild_Name + " (ID: " + _roster2.Roster_ID + ")\r\n" +
            "    Server: " + _roster2.Guild_Server + "\r\n" + "    Region: " + _roster2.Guild_Region);
        }

        MessageBox.Show("Time to Delete it");

        Business.GuildRosterController.DeleteRoster(_roster.Roster_ID);

        Business.GuildRoster _roster3;
        _roster3 = Business.GuildRosterController.GetRoster(_roster.Roster_ID);

        if (_roster3 == null) {
          MessageBox.Show("Deletion Successful.");
        } else {
          MessageBox.Show("Deletion Failed: Returned Guild:" + _roster3.Guild_Name + " (ID: " + _roster3.Roster_ID + ")\r\n" +
            "    Server: " + _roster3.Guild_Server + "\r\n" + "    Region: " + _roster3.Guild_Region);
        }



      } catch (Exception eExc) {
        MessageBox.Show(eExc.GetType().ToString() + ": \r\n" + eExc.Message.ToString());
      }
    }

    private void button3_Click(object sender, EventArgs e) {
      try {
        Business.GuildRoster _roster;
        _roster = Business.GuildRosterController.GetRoster(int.Parse(txtGuildID.Text));

        if (_roster == null) {
          MessageBox.Show("No guild rosters were returned.");
        } else {
            MessageBox.Show("Returned Guild:" + _roster.Guild_Name + " (ID: " + _roster.Roster_ID + ")\r\n" +
              "    Server: " + _roster.Guild_Server + "\r\n" + "    Region: " + _roster.Guild_Region);
          }
      } catch (Exception eExc) {
        MessageBox.Show(eExc.GetType().ToString() + ": \r\n" + eExc.Message.ToString());
      }
    }

    private void button4_Click(object sender, EventArgs e) {
      try {
        List<Business.GuildEntry> _entries;
        _entries = Business.GuildRosterController.GetCharEntries();

        if (_entries.Count == 0) {
          MessageBox.Show("No characters were returned.");
        } else {
          foreach (Business.GuildEntry _entry in _entries) {
            MessageBox.Show("Returned " + MessageBoxCharString(_entry));
          }
        }
      } catch (Exception eExc) {
        MessageBox.Show(eExc.GetType().ToString() + ": \r\n" + eExc.Message.ToString());
      }
    }


    private void button5_Click(object sender, EventArgs e) {
      try {
        Business.GuildEntry _entry;
        _entry = Business.GuildRosterController.GetCharEntry(txtCharName.Text, "Whisperwind");

        if (_entry.Char_ID <= 0) {
          MessageBox.Show("No characters were returned.");
        } else {
          MessageBox.Show("Returned " + MessageBoxCharString(_entry));

          List<GuildEntryStat> _reps = _entry.Reputations;
          List<GuildEntryStat> _resists = _entry.Resistances;
          List<GuildEntryStat> _skills = _entry.Abilities;

          foreach (Business.GuildEntryStat _stat in _skills) {
            MessageBox.Show("Returned Char Stat: " + MessageBoxCharStatString(_stat));
          }

        }
      } catch (Exception eExc) {
        MessageBox.Show(eExc.GetType().ToString() + ": \r\n" + eExc.Message.ToString());
      }
    }

    private void button6_Click(object sender, EventArgs e) {
      try {
        Business.GuildEntry _entry;
        _entry = Business.GuildRosterController.GetCharEntry(int.Parse(txtCharName.Text));

        if (_entry.Char_ID <= 0) {
          MessageBox.Show("No characters were returned.");
        } else {
          MessageBox.Show("Returned " + MessageBoxCharString(_entry));
        }
      } catch (Exception eExc) {
        MessageBox.Show(eExc.GetType().ToString() + ": \r\n" + eExc.Message.ToString());
      }
    }

    private void button7_Click(object sender, EventArgs e) {
      try {
        Business.GuildEntry _entry = new DotNetWarcraft.WoWGuildRoster.Business.GuildEntry();
        _entry.Char_Name = "Test User";
        _entry.Char_Server = "Whisperwind";
        _entry.Char_Region_Enum = WarcraftRegions.RegionUS;
        _entry.Last_Update_Date = DateTime.UtcNow;

        _entry.Char_ID = Business.GuildRosterController.AddCharEntry(_entry);

        if (_entry.Char_ID > 0) {
          MessageBox.Show("Entry Creation Succeeded");
        } else {
          MessageBox.Show("Entry Creation Failed");
        }

        Business.GuildEntry _entry2;
        _entry2 = Business.GuildRosterController.GetCharEntry(_entry.Char_ID);

        if (_entry2.Char_ID < 0) {
          MessageBox.Show("Post add: No character entries were returned.");
        } else {
          MessageBox.Show("Post add: Returned " + MessageBoxCharString(_entry2));
        }

        _entry2.Char_Name = "Test User Updated";
        _entry2.Last_Update_Date = DateTime.UtcNow;

        Business.GuildRosterController.UpdateCharEntry(_entry2);

        _entry2 = Business.GuildRosterController.GetCharEntry(_entry.Char_ID);

        if (_entry2.Char_ID < 0) {
          MessageBox.Show("Post update: No character entries were returned.");
        } else {
          MessageBox.Show("Post update: Returned " + MessageBoxCharString(_entry2));
        }

        MessageBox.Show("Time to Delete it");

        Business.GuildRosterController.DeleteCharEntry(_entry.Char_ID);

        Business.GuildEntry _entry3;
        _entry3 = Business.GuildRosterController.GetCharEntry(_entry.Char_ID);

        if (_entry3.Char_ID < 0) {
          MessageBox.Show("Deletion Successful.");
        } else {
          MessageBox.Show("Deletion Failed: Returned " + MessageBoxCharString(_entry3));
        }



      } catch (Exception eExc) {
        MessageBox.Show(eExc.GetType().ToString() + ": \r\n" + eExc.Message.ToString());
      }
    }

    private void button8_Click(object sender, EventArgs e) {
      try {
        List<Business.GuildEntryStat> _charStats;
        _charStats = Business.GuildRosterController.GetCharEntryStats();

        if (_charStats.Count == 0) {
          MessageBox.Show("No char stats were returned.");
        } else {
          foreach (Business.GuildEntryStat _stat in _charStats) {
            MessageBox.Show("Returned Char Stat: " + MessageBoxCharStatString(_stat));
          }
        }
      } catch (Exception eExc) {
        MessageBox.Show(eExc.GetType().ToString() + ": \r\n" + eExc.Message.ToString());
      }

    }

    private void button9_Click(object sender, EventArgs e) {
      try {
        Business.GuildEntry _entry = new DotNetWarcraft.WoWGuildRoster.Business.GuildEntry();
        _entry.Char_Name = "Test User";
        _entry.Char_Server = "Whisperwind";
        _entry.Char_Region_Enum = WarcraftRegions.RegionUS;
        _entry.Last_Update_Date = DateTime.UtcNow;

        _entry.Char_ID = Business.GuildRosterController.AddCharEntry(_entry);

        if (_entry.Char_ID > 0) {
          MessageBox.Show("Entry Creation Succeeded");
        } else {
          MessageBox.Show("Entry Creation Failed");
        }

        Business.GuildEntryStat _stat = new DotNetWarcraft.WoWGuildRoster.Business.GuildEntryStat();

        _stat.Char_ID = _entry.Char_ID;
        _stat.Stat_Group = "Mainland";
        _stat.Stat_Entry = "Reputation 1";
        _stat.Stat_Value_int = 432;
        _stat.Stat_Value_str = "Honored";
        _stat.Stat_Type_Enum = WarcraftStatTypes.Reputation;
        _stat.Last_Load_Date = DateTime.UtcNow;

        _stat.CharStat_ID = Business.GuildRosterController.AddCharEntryStat(_stat);

        if (_stat.CharStat_ID > 0) {
          MessageBox.Show("Entry Stat Creation Succeeded");
        } else {
          MessageBox.Show("Entry Stat Creation Failed");
        }

        Business.GuildEntryStat _statB = new DotNetWarcraft.WoWGuildRoster.Business.GuildEntryStat(_entry.Char_ID, WGRConstants.WowStatType_Int(WarcraftStatTypes.Ability), "Str", "", 18, "");
        Business.GuildEntryStat _statC = new DotNetWarcraft.WoWGuildRoster.Business.GuildEntryStat(_entry.Char_ID, WGRConstants.WowStatType_Int(WarcraftStatTypes.Ability), "Int", "", 25, "");
        Business.GuildEntryStat _statD = new DotNetWarcraft.WoWGuildRoster.Business.GuildEntryStat(_entry.Char_ID, WGRConstants.WowStatType_Int(WarcraftStatTypes.Ability), "Wis", "", 7, "");
        Business.GuildEntryStat _statE = new DotNetWarcraft.WoWGuildRoster.Business.GuildEntryStat(_entry.Char_ID, WGRConstants.WowStatType_Int(WarcraftStatTypes.Ability), "Chr", "", 12, "");
        Business.GuildEntryStat _statF = new DotNetWarcraft.WoWGuildRoster.Business.GuildEntryStat(_entry.Char_ID, WGRConstants.WowStatType_Int(WarcraftStatTypes.Ability), "Con", "", 15, "");
        _statB.CharStat_ID = Business.GuildRosterController.AddCharEntryStat(_statB);
        _statC.CharStat_ID = Business.GuildRosterController.AddCharEntryStat(_statC);
        _statD.CharStat_ID = Business.GuildRosterController.AddCharEntryStat(_statD);
        _statE.CharStat_ID = Business.GuildRosterController.AddCharEntryStat(_statE);
        _statF.CharStat_ID = Business.GuildRosterController.AddCharEntryStat(_statF);

        MessageBox.Show("Time to Edit Entry Stat");

        Business.GuildEntryStat _statEdit;
        _statEdit = Business.GuildRosterController.GetCharEntryStat(_stat.CharStat_ID);

        _statEdit.Stat_Value_int = 500;
        _statEdit.Stat_Value_str = "Revered";

        MessageBox.Show("Updating " + MessageBoxCharStatString(_statEdit));
        Business.GuildRosterController.UpdateCharEntryStat(_statEdit);
        MessageBox.Show("Completed update");

        _statEdit = Business.GuildRosterController.GetCharEntryStat(_stat.CharStat_ID);
        MessageBox.Show("Re-retrieved " + MessageBoxCharStatString(_statEdit));

        MessageBox.Show("Time to Delete Entry Stat");

        Business.GuildRosterController.DeleteCharEntryStat(_stat.CharStat_ID);

        Business.GuildEntryStat _stat2;
        _stat2 = Business.GuildRosterController.GetCharEntryStat(_stat.CharStat_ID);

        if (_stat2.CharStat_ID < 0) {
          MessageBox.Show("Entry Stat Deletion Successful.");
        } else {
          MessageBox.Show("Entry Stat Deletion Failed: Returned " + MessageBoxCharStatString(_stat2));
        }


        MessageBox.Show("Time to Delete Character");

        Business.GuildRosterController.DeleteCharEntry(_entry.Char_ID);

        Business.GuildEntry _entry2;
        _entry2 = Business.GuildRosterController.GetCharEntry(_entry.Char_ID);

        if (_entry2.Char_ID < 0) {
          MessageBox.Show("Character Deletion Successful.");
        } else {
          MessageBox.Show("Character Deletion Failed: Returned " + MessageBoxCharString(_entry2));
        }



      } catch (Exception eExc) {
        MessageBox.Show(eExc.GetType().ToString() + ": \r\n" + eExc.Message.ToString());
      }
    }

    private void button10_Click(object sender, EventArgs e) {
        WoWGuildRoster.Business.LoadArmoryRoster _loadArmory = new LoadArmoryRoster(1);

      Cursor.Current = Cursors.WaitCursor;
      _loadArmory.Execute();

      Cursor.Current = Cursors.Default;

    }

    private void button11_Click(object sender, EventArgs e) {
      List<WoWGuildRoster.Business.QuickStatListItem> _stats = Business.GuildRosterController.GetEntryStatsLIs_ByStatEntryAndTypeAndGroup(txtStatEntry.Text, txtStatGroup.Text, int.Parse(txtStatType.Text), false);


      MessageBox.Show("Retrieved " +  _stats.Count.ToString() + " items");

      foreach (WoWGuildRoster.Business.QuickStatListItem _stat in _stats) {
        MessageBox.Show("Character " + _stat.Char_Name + ":" + "\r\n" +
          "    Stat: " + _stat.Stat_Value_str + " (string); " + _stat.Stat_Value_int.ToString() + " (int); " + _stat.Stat_Value_dec.ToString() + " (dec)" + "\r\n" );
      }
    }

    private void button12_Click(object sender, EventArgs e) {
      try {
        List<Business.GuildEntry> _entries;
        _entries = Business.GuildRosterController.GetCharEntries(int.Parse(txtGuildID.Text));

        if (_entries.Count == 0) {
          MessageBox.Show("No characters were returned.");
        } else {
          foreach (Business.GuildEntry _entry in _entries) {
            //MessageBox.Show("Returned " + MessageBoxCharString(_entry));
          }
        }

        //List<QuickStatListItem> _alchems;
        //_alchems = Business.GuildRosterController.GetCharEntriesByProf(int.Parse(txtGuildID.Text), "Enchanting");

        //if (_alchems.Count == 0) {
        //  MessageBox.Show("No characters were returned.");
        //} else {
        //  foreach (Business.QuickStatListItem _alch in _alchems) {
        //    //MessageBox.Show("Returned " + MessageBoxCharString(_entry));
        //  }
        //}


      } catch (Exception eExc) {
        MessageBox.Show(eExc.GetType().ToString() + ": \r\n" + eExc.Message.ToString());
      }

    }
  }
}